package com.thkj.modules.sys.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.thkj.common.util.RequestUtil;
import com.thkj.modules.sys.entity.TblTown;
import com.thkj.modules.sys.service.TblTownService;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import dp.R;
import dp.dao.PagingList;
import dp.web.BaseAction;

/**
 * 镇管理
 * @author weihuu
 *
 */
@RestController
@RequestMapping("/backstage/town")
public class TownAction extends BaseAction{
    @Autowired
    TblTownService tblTownService;
    
    @RequestMapping("")
    public ModelAndView index(){
    	return new ModelAndView("town/list");
    }
	
	@RequestMapping("/query")
    public R query(HttpServletRequest request, int page, int size){
    	Map<String, Object> param = RequestUtil.getParameterMap(request);
    	PagingList<Map> list = tblTownService.query(page, size,param);
    	return R.success(list);
    }
	
    @RequestMapping("/edit")
    public R edit(TblTown town){
    	try {
    		if(StrUtil.isBlank(town.getId())) {
    			town.setId(IdUtil.objectId());
    			tblTownService.insertTblTown(town);
        	}else {
        		tblTownService.updateTblTown(town);
        	}
        	return R.SUCCESS;
    	}catch (Exception e) {
			return R.failure(e.getMessage());
		}
    }
    
    @RequestMapping("/delete")
    public R delete(String id){
    	tblTownService.deleteTblTown(id);
    	return R.SUCCESS;
    }
    
    /**
     * 查询区域（区镇dto）
     * @return
     */
    @RequestMapping("/queryRegion")
    public R queryRegion(){
    	return R.success(tblTownService.queryRegion());
    }
}